import {
  getBuildRent,
  getBuildSale,
  getShangSale,
  getShangRent,
} from '@/api/getShop';
// import getBuildSaleList   from '@/data/fang_office.json'
export interface IndexModelType {
  namespace: 'shop';
  state: any;
  effects: {};
  reducers: {};
  subscriptions: any;
}

const IndexModel: IndexModelType = {
  namespace: 'shop',
  state: {
    list: [],
    editlist:{}
  },

  effects: {
    *init(action: any, { call, put }) {
      // const {} = action;
      const data = yield call(getBuildSale);
      yield put({ type: 'GET_LIST', list: data.data.data });
    },
  },
  reducers: {
    GET_LIST(state: any, action: any) {
      state.list = action.list;
      return {
        ...state,
      };
    },

    SEARCH(state: any, action: any) {
      const { value } = action;
      const arr = JSON.parse(JSON.stringify(state.list));
      const newarr: any = [];
      arr.filter((item: any, index: any) => {
        if (item.title.includes(value)) {
          return newarr.push(item);
        }
      });
      state.list = newarr;
      return {
        ...state,
      };
    },

    DEL(state: any, action: any) {
      console.log(action);

      const arr = JSON.parse(JSON.stringify(state.list));
      let index = arr.findIndex((item: any) => item.id == action.id);
      arr.splice(index, 1);
      state.list = arr;
      return {
        ...state,
      };
    },

    ADD(state: any, action: any) {
      const arr = JSON.parse(JSON.stringify(state.list));
      arr.push({
        id: state.list.length + 2,
        estate_name: action.values.estate_name,
        title: action.values.title,
        price: action.values.price,
        acreage: action.values.acreage,
        status: 1,
        ordid: 1000,
      });
      state.list = arr;
      return {
        ...state,
      };
    },
    
    BUILDSALEEDIT(state: any, action: any){
      const arr = JSON.parse(JSON.stringify(state.list));
      const newarr = arr.find((item:any)=>item.id == action.id)
      state.editlist = newarr
      return {
        ...state
      }
    },

    SETEDITBUILDSALE(state: any, action: any){
      console.log(action);
      const arr = JSON.parse(JSON.stringify(state.list));
      arr.forEach((item:any,index:number)=>{
        if(item.id === action.values.id){
          console.log(11)
          arr[index] = action.values
        }
      })
      state.list = arr
      return {
        ...state
      }
    }

  },
  subscriptions: {
    setinit({ dispatch, history }: any) {
      dispatch({
        type: 'init',
      });
    },
  },
};

export default IndexModel;
